/*
 * 软件版权: 恒生电子股份有限公司
 * 修改记录:
 * 修改日期     修改人员  修改说明
 * ========    =======  ============================================
 * 2020/7/31  lsy13  新增
 * ========    =======  ============================================
 */
package com.sansi.pencilbook.util;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sansi.pencilbook.DO.ResponseDO;
import lombok.extern.slf4j.Slf4j;

/**
 * 功能说明:
 *
 * @author lsy13
 */
@Slf4j
public class ResponseDOUtil {

  /**
   * 返回带有data的ResponseDO
   * @param isSuccess
   * @param object
   * @return
   */
  public static ResponseDO setResponseDO(boolean isSuccess, Object object){
    ObjectMapper mapper = new ObjectMapper();
    String data = null;
    try{
      data = mapper.writeValueAsString(object);
    }catch (Exception e){
      e.printStackTrace();
      log.error("返回对象json化失败");
      return new ResponseDO(false,"fail",0,data);
    }
    return isSuccess ? new ResponseDO(true,"success",1,data) :
        new ResponseDO(false,"fail",0,data);
  }

  /**
   * 返回带有data和msg的ResponseDO
   * @param isSuccess
   * @param object
   * @return
   */
  public static ResponseDO setResponseDO(boolean isSuccess, String msg, Object object){
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
    String data = null;
    try{
      data = mapper.writeValueAsString(object);
    }catch (Exception e){
      e.printStackTrace();
      log.error("返回对象json化失败");
      return new ResponseDO(false,"对象json化失败",0,data);
    }
    return isSuccess ? new ResponseDO(true, msg,1,data) :
            new ResponseDO(false, msg,0,data);
  }

  /**
   * 返回不带有data和msg的ResponseDO
   * @param isSuccess
   * @return
   */
  public static ResponseDO setResponseDO(boolean isSuccess){
    return isSuccess ? new ResponseDO(true,"success",1,null) :
        new ResponseDO(false,"fail",0,null);
  }

  /**
   * 返回带有msg的ResponseDO
   * @param isSuccess
   * @param msg
   * @return
   */
  public static ResponseDO setResponseDO(boolean isSuccess, String msg){
    return isSuccess ? new ResponseDO(true,msg,1,null) :
        new ResponseDO(false,msg,0,null);
  }
}
